package com.gzx.plugin.tjzy.modular.sms.receipt.mapper;

import com.gzx.plugin.tjzy.modular.sms.receipt.bo.GzxStatisticSmsReceiptBo;
import com.gzx.plugin.tjzy.modular.sms.receipt.entity.GzxSmsReceipt;
import com.gzx.plugin.tjzy.modular.sms.receipt.vo.GzxSmsReceiptStatisticVo;
import com.gzx.plugin.tjzy.modular.sms.receipt.vo.GzxSmsReceiptVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.common.mapper.BaseMapperPlus;

import java.util.List;
import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/3/11 15:13
 */
public interface GzxSmsReceiptMapper extends BaseMapperPlus<GzxSmsReceipt, GzxSmsReceiptVo> {

    @Insert("<script>" +
            "INSERT INTO gzx_sms_receipt " +
            "(batch_number,project_code,supplier_code, flag_type, phone,send_time, msg_id,mapping_status,result_code, " +
            "result_name, all_detail, tenant_id) " +
            "VALUES ${sql}" +
            "</script>")
    void insertBatchSql(String sql);

    @Select("<script>" +
            "SELECT result_code AS resultCode, COUNT(1) AS count " +
            "FROM (" +
            "SELECT DISTINCT msg_id, phone, result_code " +
            "FROM gzx_sms_receipt " +
            "WHERE batch_number = #{batchNumber} " +
            ") AS subquery " +
            "GROUP BY resultCode" +
            "</script>")
    List<Map<String, Object>> countByBatchNumber(String batchNumber);

    //短信送达率
    @Select("<script>" +
            "SELECT project_code as projectCode ," +
            " count(if(mapping_status = 1, 1, null)) as successSend, " +
            " count(*) as totalSend " +
            " FROM gzx_sms_receipt " +
            " WHERE create_time between #{beginTime} and #{endTime} " +
            " <if test='projectCode != null and projectCode != \"\"'>" +
            "   and project_code = #{projectCode} " +
            " </if>" +
            " group by project_code" +
            " limit #{current},#{size} " +
            "</script>")
    List<GzxSmsReceiptStatisticVo> smsSendRate(GzxStatisticSmsReceiptBo bo);

    //短信送达率总数
    @Select("<script>" +
            "select count(*) from ( " +
            " SELECT project_code as projectCode ," +
            " count(if(mapping_status = 1, 1, null)) as successSend, " +
            " count(*) as totalSend " +
            " FROM gzx_sms_receipt " +
            " WHERE create_time between #{beginTime} and #{endTime} " +
            " <if test='projectCode != null and projectCode != \"\"'>" +
            "   and project_code = #{projectCode} " +
            " </if>" +
            " group by project_code" +
            " ) t" +
            "</script>")
    long smsSendRateTotal(GzxStatisticSmsReceiptBo bo);
}
